//管理user功能相关状态数据的vuex模块
import {RECIVE_ADDRESS,RECIVE_CATEGORYS,RECIVE_SHOPS} from '../mutation-types'
import {
    reqAddress,
    reqShops,
    reqCategorys,
} from '../../request/request.js'

const state={
    latitude:'40.10038',//精度
    longitude:'116.36867',//纬度
    address:'',//地址信息对象
    category:[],//分类的数组
    shops:[],//商家的数组
}
const mutations={
   
    [RECIVE_CATEGORYS](state,category){
        state.category = category
    },
    [RECIVE_SHOPS](state,shops){
        state.shops = shops
    },
    [RECIVE_ADDRESS](state,address){
        console.log(address);
        state.address= address
    },
}
const getters={}
const actions={
     //获取当前信息的异步action
     async getAddress({commit,state}){
        //1/调用接口发送请求
        const {latitude,longitude} = state
        console.log(latitude,longitude);
        const result = await reqAddress(latitude,longitude)
        //2.有了结果给mutation
        if (result.code===0) {
            const address = result.data
            commit(RECIVE_ADDRESS,address)
        }
    },
    
    //获取商品分类列表
    async getcategorys({commit,state}){
        const result = await reqCategorys()
        //2.有了结果给mutation
        if (result.code===0) {
            const category = result.data
            commit(RECIVE_CATEGORYS,category)
        }
    },
    //获取商家列表
    async getShops({commit,state}){
        //1/调用接口发送请求
        const {latitude,longitude} = state
        const result = await reqShops(latitude,longitude)
        //2.有了结果给mutation
        if (result.code===0) {
            const shops = result.data
            commit(RECIVE_SHOPS,shops)
        }
    },
}
export default{
    state,
    mutations,
    actions,
    getters
}